const app = getApp()
import * as API_Common from '../../../api/common'
import * as API_Safe from '../../../api/safe'
import * as API_members from '../../../api/members'
import { Foundation, RegExp } from '../../../ui-utils/index.js'
import { md5 } from '../../../lib/md5'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    forword: '',
    uuid: '',
    mobile: '',
    /*  step: 1, */
    /* 是否有密码 */
    /*  isApyPassword: true,
     type:true, */
    //效验手机号表单
    validMobileForm: {
      password: '',
      rep_password: '',
      sms_code: ''
    },
    //valid_img_url: '',//图片验证码URL
    //修改密码表单
    /*  changePasswordForm: {
       password: '',
       rep_password: '',
       img_code: ''
     }, */
    initTip: '发送验证码',
    endTip: '重新获取',
    sufTip: '秒后重新获取',
    count_time: 60,
    disabled: false,
    message: '',
    interval: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.type) {
      this.setData({ forword: options.type })
    }
    const uuid = wx.getStorageSync('uuid')
    const user = wx.getStorageSync('user')
    this.setData({
      uuid: uuid,
      mobile: Foundation.secrecyMobile(user.mobile),
    })
    /* this.getValidImgUrl() */
    /*  API_members.seeApyPassword().then(res => {
       this.setData({ isApyPassword: res })
     }) */
  },

  onPassword(e) {
    this.setData({ 'validMobileForm.password': e.detail.value })
  },
  syncSmsCode(e) {
    this.setData({ 'validMobileForm.sms_code': e.detail.value })
  },
  onRep_password(e) {
    this.setData({ 'validMobileForm.rep_password': e.detail.value })
  },
  // 短信验证码时间
  startCountDown() {
    this.setData({ disabled: true })
    this.data.interval = setInterval(() => {
      if (this.data.count_time > 1) {
        this.data.count_time--
        this.data.message = this.data.count_time + this.data.sufTip
        this.setData({
          count_time: this.data.count_time,
          message: this.data.message
        })
      } else {
        clearInterval(this.data.interval)
        this.setData({
          message: '重新发送',
          count_time: 60,
          disabled: false
        })
      }
    }, 1000)
  },
  /* 发送验证码 */
  sendValidMobileSms() {
    if (this.check()) {
      API_members.sendCode({ captcha: '0000', uuid: this.data.uuid }).then(res => {
        wx.showToast({ title: '验证码发送成功！' })
        this.startCountDown();
      })
    }
  },
  //提交校验
  submitValMobileForm() {
    console.log(getCurrentPages())
    const sms_code = this.data.validMobileForm.sms_code
    let validMobileForm = this.data.validMobileForm
    validMobileForm.password=md5(validMobileForm.password)
    this.setData({
      validMobileForm
    })
    API_members.addApyPassword(this.data.validMobileForm).then(res => {
      wx.showToast({ title: '设置成功！' })
      setTimeout(() => {
        console.log(this.data)
        if (this.data.forword == "shop_pay" || this.data.forword == "cashier") {
          wx.navigateBack({
            delta: 1
          });
        } else {
          wx.navigateBack({
            delta:2
          })
          // wx.switchTab({
          //   url: '/pages/ucenter/index/index'
          // })
        }
      }, 800);
    })
  },
  //校验
  check() {
    const { password, rep_password } = this.data.validMobileForm
    if (!password) {
      wx.showToast({ title: '请填写支付密码！', icon: 'none' })
      return false
    }
    if (!/^\d{6}$/.test(password)) {
      wx.showToast({ title: '密码格式必须是6位数字', icon: 'none' })
      return false
    }
    if (!rep_password) {
      wx.showToast({ title: '请再次填写支付密码！', icon: 'none' })
      return false
    }
    if (password !== rep_password) {
      wx.showToast({ title: '两次密码输入不一致', icon: 'none' })
      return false
    }
    return true;
  },

})